#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <windows.h>
#include "sha512.h"    //Thanks Taha Canturk's SHA-512 algorithm
using namespace std;
class bankUser{
	public:
		double moneys;
		double owedMoneys;
		string userName;
		string password;                       //sha-512ed
		string passwordEncrypted;
		void initzation();
		void moneyOut(double);
		void moneyIn(double);
};
void bankUser::initzation(){
	cout<<"YOU ARE REGSITER A BANK USER.\n";
	moneys=0.0;
	owedMoneys=0.0;
	string passwordInputed[2];
	string userNameF;
	cout<<"Input your user name "<<":";
	cin>>userNameF;
	userName = userNameF;
	cout<<"Input your user password, "<<userName<<":";
	cin>>passwordInputed[0];
	cout<<"Input your user password again, "<<userName<<":";
	cin>>passwordInputed[1];
	if(passwordInputed[0]==passwordInputed[1]);
	passwordEncrypted = sha512(passwordInputed[0]);
	system("cls");
}
void bankUser::moneyOut(double moneysForOut){
	string passwordInputed;
	cout<<"Input your password, "<<userName<<":";
	cin>>passwordInputed;
	if(sha512(passwordInputed)==passwordEncrypted){
		cout << userName <<",you have: " << (moneys-owedMoneys)<< " now.\n";
		cout << "Will out:" << moneysForOut << endl;
	} else {
		cout << "Wrong password.You made a mistake.\n";
		return;
	}
	moneys -= moneysForOut;
	if(moneys<0){
		moneys = abs(moneys);
		owedMoneys += moneys;
		moneys = 0;
	}
	cout << "You have: "<< (moneys-owedMoneys)<< " now.\n";
	system("pause");
	system("cls");
}
void bankUser::moneyIn(double moneyIned){
	string passwordInputed;
	cout<<"Input your password, "<<userName<<":";
	cin>>passwordInputed;
	if(sha512(passwordInputed)==passwordEncrypted){
		cout << userName <<",you are put in now.\nYou have: "<< (moneys-owedMoneys)<< " now.\n";
		cout << "Will in:" << moneyIned << endl;
	} else {
		cout << "Wrong password.You made a mistake.\n";
		return;
	}
	if(owedMoneys > 0){
		for(;moneyIned>=0;moneyIned-=1){
			owedMoneys-=1;
			if(owedMoneys==0){
				break;
			}
		}
		if(moneyIned==0){
			return;
		} else {
			moneys += moneyIned;
		}
	} else {
		moneys += moneyIned;
	}
	cout << "You have: "<< (moneys-owedMoneys)<< " now.\n";
	system("pause");
	system("cls");
}
void pass(){};

int main(){
	bankUser user1,user2;
	user1.initzation();
	user2.initzation();
	user1.moneyOut(3500.0);
	user2.moneyIn(3500.0);
	user1.moneyIn(14320.0);
	user2.moneyOut(14320.0);
	return 0;
}

